<?php


namespace app\common\service\order\coupon_order\jutuike_channel;

use app\common\service\finance\FinanceManager;
use app\common\service\finance\FinanceScene\SupplierFailGoBack;
use app\model\CouponBrand;
use app\model\CouponOrder;
use think\facade\Db;
use think\facade\Log;

/**
 * 统一订单
 * Class Vip
 * @package app\common\service\order\coupon_order\jutuike_channel
 */
class Union extends JutuikeChannelBase
{

    public $pullOrders = [
        47 => PRODUCT_TRAVEL_OIL_SERV,//高德
        42 => PRODUCT_TRAVEL_OIL_SERV,//出行，加油
        87 => PRODUCT_TRAVEL_OIL_SERV,//同程打车
        49 => PRODUCT_TRAVEL_OIL_SERV,//花小猪
        63 => PRODUCT_TRAVEL_OIL_SERV,//花小猪司机
        43 => PRODUCT_TRAVEL_OIL_SERV,//滴滴加油
        44 => PRODUCT_TRAVEL_OIL_SERV,//货运
//        53 => PRODUCT_TRAVEL_OIL_SERV,//滴滴代驾CPS活动
        58 => PRODUCT_TRAVEL_OIL_SERV,//小桔加油
        67 => PRODUCT_TRAVEL_OIL_SERV,//滴滴有车
//        50 => PRODUCT_COUPON_SERV,//会员卡券 在同级目录下Vip拉取单独处理


        //周边游
        54 => PRODUCT_AROUND_SHOP_SERV, //联联周边游
        62 => PRODUCT_AROUND_SHOP_SERV, //旅划算周边游

        //餐饮
        57 => PRODUCT_FOOD_SHOP_SERV, //聚合点餐


        46 => PRODUCT_FOOD_SHOP_SERV,//汉堡王
        34 => PRODUCT_FOOD_SHOP_SERV, //星巴克
        26 => PRODUCT_FOOD_SHOP_SERV, //麦当劳
        16 => PRODUCT_FOOD_SHOP_SERV, //肯德基
        31 => PRODUCT_FOOD_SHOP_SERV, //百果园
        32 => PRODUCT_FOOD_SHOP_SERV, //奈雪的茶
        33 => PRODUCT_FOOD_SHOP_SERV, //瑞幸咖啡
        37 => PRODUCT_FOOD_SHOP_SERV, //喜茶
        38 => PRODUCT_FOOD_SHOP_SERV, //肯德基活动2
        45 => PRODUCT_FOOD_SHOP_SERV, //美团外卖优质商家
        64 => PRODUCT_FOOD_SHOP_SERV, //必胜客
        4 => PRODUCT_ELE_SERV, //饿了么餐饮红包CPS
        5 => PRODUCT_ELE_SERV, //饿了么生鲜CPS推广活动
        3 => PRODUCT_ELE_SERV, //美团闪购CPS+CPA推广活动
        1 => PRODUCT_FOOD_SHOP_SERV, //美团外卖CPS推广活动 ①
        2 => PRODUCT_FOOD_SHOP_SERV, //美团闪购CPS+CPA推广活动
        28 => PRODUCT_FOOD_SHOP_SERV, //美团酒店CPS推广活动
        9 => PRODUCT_FOOD_SHOP_SERV, //美团到店优惠活动
        27 => PRODUCT_FOOD_SHOP_SERV, //美团周末特价活动
        7 => PRODUCT_FOOD_SHOP_SERV, //美团外卖CPS推广活动
        8 => PRODUCT_FOOD_SHOP_SERV, //美团闪购CPS推广活动

    ];

    public $act_id = 0; //聚推客的活动类型，

    public function getServType()
    {


//        $arr = $this->pullOrders;

        $arr = CouponBrand::query()
            ->where('belong', CouponBrand::BELONG_JUTUIKE)
            ->cache('CouponBrandbelongcache',130)
            ->whereNotNull('serv_type')
            ->column('serv_type', 'act_id');

        if (empty($this->params['act_id'])) return '';

        if ($this->params['act_id'] == 50) return '';

        if (empty($arr[$this->params['act_id']]) && $this->params['act_id'] != 50) {//会员卡券单独处理
            Log::error('没有找到相对应的act_id:' . $this->params['act_id']);
            return '';
        };

        if ($this->extend->supplierInfo->serv_type != $arr[$this->params['act_id']]) {
//            echo $this->extend->supplierInfo->serv_type.'和'.$arr[$this->params['act_id']].'不相等'.PHP_EOL;
            return '';
        }; //不是该供应商类型的直接过滤了

        $this->act_id = $this->params['act_id'];

        return $arr[$this->params['act_id']];
    }



    /**
     * @return CouponOrder|bool|mixed
     */
    public function syncOrder()
    {

        //没有在开发计划中的佣金不结算
        if (empty($this->getServType())) {
            return false;
        }
//        1：已付款 2：待结算 3：已结算 4：无效订单
        if (!in_array($this->params['status'], [1, 2, 3, 4])) {
            return false;
        }

        //状态允许的时候再保存数据
        /**
         * @var CouponOrder $couponOrder
         */
        $couponOrder = $this->saveOrder();


        if ($couponOrder->profit != $this->profit){
            $couponOrder->profit = $this->profit;
            $couponOrder->log .= date('Y-m-d H:i:s') . '修改佣金从'.$couponOrder->profit.'到'.$this->profit;
            $couponOrder->save();
        }

        if ($couponOrder->platform_profit != $this->platform_profit){
            $couponOrder->platform_profit = $this->platform_profit;
            $couponOrder->log .= date('Y-m-d H:i:s') . '修改平台佣金从'.$couponOrder->platform_profit.'到'.$this->platform_profit;
            $couponOrder->save();
        }

        //2023年2月16日10:56:10 去掉 && !empty($couponOrder->profit) 因为长时间为待结算状态
        if ($this->params['status'] == 3 && $couponOrder->agent_settle_status == CouponOrder::C_AGENT_SETTLE_STATUS_NO && $couponOrder->profit >= 0 ) {
            $couponOrder->log .= date('Y-m-d H:i:s') . '修改为待结算';
            $couponOrder->agent_settle_status = CouponOrder::C_AGENT_SETTLE_STATUS_WAIT;
            $couponOrder->save();
        }

        if (
            in_array($couponOrder->agent_settle_status,[CouponOrder::C_AGENT_SETTLE_STATUS_SUCCESS,CouponOrder::C_AGENT_SETTLE_STATUS_WAIT])
            && empty($couponOrder->order_finish_at)
        ){
            $couponOrder->log .= date('Y-m-d H:i:s') . '填补完成时间';
            $couponOrder->order_finish_at = date('Y-m-d H:i:s');
            $couponOrder->save();
        }


        //付款之后变成无效订单
        if ($this->params['status'] == 4 && $couponOrder->agent_settle_status == CouponOrder::C_AGENT_SETTLE_STATUS_WAIT) {
            $couponOrder->log .= date('Y-m-d H:i:s') . '待结算改为无效订单';
            $couponOrder->status = CouponOrder::C_STATUS_RECHARGE_FAIL;
            $couponOrder->refund_status = CouponOrder::C_REFUND_STATUS_HAVE; //已退款筛选
            $couponOrder->agent_settle_status = CouponOrder::C_AGENT_SETTLE_STATUS_NO;
            $couponOrder->save();
        }

        //结算之后变成无效订单
        if ($this->params['status'] == 4 && $couponOrder->agent_settle_status == CouponOrder::C_AGENT_SETTLE_STATUS_SUCCESS) {
            $couponOrder->log .= date('Y-m-d H:i:s') . '已结算改为无效订单 倒扣';
            $couponOrder->status = CouponOrder::C_STATUS_RECHARGE_FAIL;
            $couponOrder->refund_status = CouponOrder::C_REFUND_STATUS_HAVE; //已退款筛选
            $couponOrder->agent_settle_status = CouponOrder::C_AGENT_SETTLE_STATUS_NO;
            $couponOrder->save();

            FinanceManager::handle(new SupplierFailGoBack($couponOrder));//倒扣钱

        }

        //直接就是废的
        if ($this->params['status'] == 4 && $couponOrder->status == CouponOrder::C_STATUS_RECHARGE_SUCCESS) {
            $couponOrder->status = CouponOrder::C_STATUS_RECHARGE_FAIL;
//            $couponOrder->refund_status = CouponOrder::C_REFUND_STATUS_HAVE; //无效订单 不一定是退款的  订单统一状态，0：未付款 1：已付款 2：待结算 3：已结算 4：无效订单
            $couponOrder->save();
        }


        return $couponOrder;

    }


    public function getProductName()
    {
        return $this->params['order_title'] ?? '';
    }

    public function getProductImage()
    {
        return $this->params['icon'] ?? '';
    }

    /**
     * 代理结算状态
     */
    public function getAgentSettleStatus()
    {
        //聚推客
        return $this->params['status'] == 3 ? CouponOrder::C_AGENT_SETTLE_STATUS_WAIT : CouponOrder::C_AGENT_SETTLE_STATUS_NO;
    }

    public function getStatus()
    {
        //拉取的都是充值成功的
        return CouponOrder::C_STATUS_RECHARGE_SUCCESS;
    }

    //数据哪个品牌id
    public function getBrandId()
    {
        if ($this->act_id){
            /**@var CouponBrand $couponBrand***/
            $couponBrand = CouponBrand::query()
                ->where('act_id', $this->act_id)
                ->find();

            if ($couponBrand){
                return  $couponBrand->id;
            }

        }

        return 0;
    }


}